<?php


class tx_zlib_viewhelper_fileInfo extends tx_zlib_abstract_viewhelper
{

	/**
	 * Returns a string with info about file extension and size.
	 *
	 * @param string $relFilePath
	 * @param boolean $noBrackets
	 * @return string
	 */
	public function fileInfo($relFilePath, $noBrackets = false) {
		if(is_file($relFilePath)) {
			$parts = pathinfo($relFilePath);
			$filesizeString = '';
			$filesizenames = array("Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
			$filesize = filesize($relFilePath);
			if($filesize > 0) {
				$i = floor(log($filesize, 1024));
				$exponentialValue = pow(1024, ($i));
				$decimals = min(1, $i);
				$filesizeString = round($filesize/$exponentialValue, $decimals) . $filesizenames[$i];
			} else {
				$filesizeString = '0' . $filesizenames['0'];
			}
			if(empty($parts['extension'])) {
			    if($noBrackets) {
				    return sprintf('<span class="fileinfo" style="white-space:nowrap">%s</span>', $filesizeString);
			    } else {
				    return sprintf('<span class="fileinfo" style="white-space:nowrap">(%s)</span>', $filesizeString);
			    }
			} else {
			    if($noBrackets) {
				    return sprintf('<span class="fileinfo" style="white-space:nowrap">%s, %s</span>', strtoupper($parts['extension']), $filesizeString);
			    } else {
				    return sprintf('<span class="fileinfo" style="white-space:nowrap">(%s, %s)</span>', strtoupper($parts['extension']), $filesizeString);
			    }
			}
		}
	}

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_fileInfo.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_fileInfo.php']);
}
